home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / 4th86_v4.zip / ARRAY.4TH < prev    next >
Text File  |  1994-01-01  |  6KB  |  191 lines

  1. ( forget strt
  2. : strt ; )
  3. off printload 
  4. unsplit
  5.  
  6. ( **************************************************** )
  7. ( * this file contains an example of a defining word * )
  8. ( *                                                  * )
  9. ( * source  extended  from the original CP/M80       * )
  10. ( **************************************************** )
  11.  
  12.  
  13. * Most FORTH words execute some function -- such as drawing a line;
  14. *  storing a variable in memory; saving a file etc.
  15. *
  16. *  COMPILER WORDS are different in that their effect is to extend the
  17. *  scope of the FORTH interpreter itself by creating ( or part creating )
  18. *  a new word.
  19. *
  20. *  The existing words CODE; CONSTANT; BLOCK; -- and of course the colon
  21. *  and semicolon that start and end all definitions -- are compiler words.
  22. *
  23. *  What this file does is illustrate how to write your own compiler words.
  24. *  The word chosen is ARRAY -- which functions in much the same manner as 
  25. *  BLOCK.
  26. *
  27. *    2 BLOCK FRED  for example creates a storage are of 2 bytes which 
  28. *  is accessible by the name FRED.
  29. *
  30. *       3 4 ARRAY KEN  will be a word which creates a storage area of
  31. *  ( 3 x 4 ) = 12 bytes-pairs [ ie 16 bits ]  -- and allows cells to be 
  32. *  read again by a command such as   2 3 KEN @
  33. *
  34. *  The defined word, when executed, returns the address
  35. *  of specified cell (tos is row, nos is column)
  36. *  Each cell is 16 bits.  For example,
  37. *
  38. *        8 3 ARRAY XYZ   ( 3 rows (0 to 2), 8 columns (0 to 7))
  39. *        0 4 0 XYZ !     ( store 0 in 0th row, 4th column)
  40. *        7 2 XYZ @       ( fetch last cell in array)
  41. *
  42. *  No error checking at compile time or run time, add it if needed.
  43. *
  44. * ARRAY designed to build the following code:
  45. *        CALL xarray   ;call runtime, tos will have adr of dw's
  46. *        DW   base     ;base of data space allocated
  47. *        DW   #rows-1
  48. *        DW   #cols-1  
  49. * )
  50.  
  51. : ARRAY ( col row -- word)
  52.   0 DEFINE  ( put name in dictionary)
  53.   e8H HEADB! ( opcode for CALL )
  54.  
  55.   ' XARRAY   ( XARRAY not defined yet - no problem though as 
  56.               in high mode, tick (') does not try to look up
  57.           the address of following word.  Instead, it 
  58.               stores text string "XARRAY" in-line so word can 
  59.           be looked up ( in the appropriate dictionary ) when 
  60.           ARRAY executes )
  61.  
  62.   head @ 2+ - HEAD! ( relative call address of XARRAY )
  63.  
  64.   OVER OVER * 2 * LUM @ SWAP - DUP LUM ! ( alloc memory)
  65.   HEAD! ( store base address after call)
  66.   HEAD! ( store number rows) ( for later error checking)
  67.   2 * HEAD! ( store # bytes/row )
  68. ; IMMEDIATE
  69.  
  70.  
  71. ( ---------------------------------------------------- )
  72.                ( run time for ARRAY)
  73.  
  74. (
  75. * this is comparable to the exiting words VLOAD, LVLOAD etc. It
  76. *  extracts the address pointer to the data from the array.
  77. )
  78.  
  79. : XARRAY ( col row -- adr)
  80.   SWAP OVER 4 + @ ( get 'column*2' dimension)
  81.   * ( offset to correct row)
  82.   SWAP @ ( get array base adr) + ( baseadr + rowofset)
  83.   SWAP DUP + ( column offset) + ( got adr of cell)
  84. ;
  85.  
  86. (    **** NOTE *** 
  87.  
  88. *  If you are creating a standalone turnkey COM file that uses the word
  89. *  ARRAY - then it must be pre-loaded into the "mother" system you are 
  90. *  using to cross-compile the standalone COM file. 
  91. *
  92. *  While it is **NOT** needed in the actual standalone COM file ( Only the 
  93. *  routine XARRAY is needed there ) it will do no harm to include it.
  94. *
  95. *  You will need XARRAY in the mother system as well as ARRAY to be able 
  96. *  to use ARRAY during [ non cross compile ] development and debugging.
  97. )
  98.  
  99. ( ---------------------------------------------------- )
  100.  
  101. on printload
  102. cls
  103.  
  104. ( Following is an illustration of the use of ARRAY 
  105.  
  106. The word SHOWCALENDAR is used to display dates against days.
  107.     It takes two parameters --  X Y SHOWCALENDAR
  108. where X is a number from 0 through 6 to represent the starting day
  109.       Y is a number from 28 through 31 to represent the days in the month
  110.  
  111.      so  4 30 SHOWCALENDAR will display 30 days starting on Wednesday )
  112.  
  113. off printload
  114.  
  115. ( ** NOTE ** there is no check on the values of X and Y inserted. Impossible 
  116. values such as days > 31 or starting day > 6 will be accepted. Modify as you 
  117. wish to check the  input parameters for valid range )
  118.  
  119. : HEADER  crlf "  Su  Mo  Tu  We  Th  Fr  Sa  " ." crlf
  120.            " ============================ " ."  ;
  121.  
  122.   7 5 array calendar
  123.   2 block startday
  124.   2 block maxdate
  125.  
  126. ( ***********
  127.   fill array with  numbers 0 through maxdate
  128.   fill actually starts with a negative value ( 0 - startdate )
  129.   and where a negative value would have been inserted, 40 is inserted
  130.   instead. [ 40 is arbitrary - any number greater than 31 will do ]
  131.    40 is treated specially in SHOWC which follows
  132.   *********** )
  133.  
  134. : fillc 0 startday @ -  
  135.           4 0                 ( rows ) 
  136.             do 6 0             ( columns ) 
  137.             │   do 1+ dup 
  138.         │   │    dup 1 <         ( previous month )
  139.         │   │  if          
  140.         │   │  │   drop 40         ( so put in a 40 )
  141.         │   │ then 
  142.         │   │   i j  calendar ! 
  143.         │  loop 
  144.            loop   drop ;
  145.  
  146.         
  147. ( ***********
  148.   fill array with  
  149.   arbitrary value 40
  150.   *********** )
  151.  
  152. : clear 4 0 
  153.     do 6 0 
  154.     │    do  
  155.         │    │ 40 i j calendar ! 
  156.         │   loop 
  157.        loop ;
  158.  
  159. ( ***********
  160.   display array on screen
  161.   *********** )
  162.  
  163. : showc 4 0                     ( rows )
  164.     do crlf 6 0                 ( columns )
  165.     │    do i j calendar @ dup 10 <  
  166.     │    │  if 
  167.         │    │  │ 1 spcs       ( extra leading space for single digits )
  168.         │    │ then dup maxdate @  > 
  169.         │    │    if 
  170.         │    │       │  2 spcs drop 0 .c 1 spcs ( print blank space character )
  171.         │    │    else 
  172.         │    │    │ . 1 spcs        ( print valid date )
  173.         │    │    then    
  174.         │   loop 
  175.        loop ;
  176.  
  177. ( ***********
  178.   main routine
  179.   *********** )
  180.  
  181. : showcalendar maxdate ! startday ! 
  182.         clear header fillc showc ;
  183.  
  184. " __________________________________________________________ " ." crlf crlf
  185. "                                  3 30 showcalendar " ."
  186. 3 30 showcalendar crlf crlf crlf
  187.  
  188.  
  189.